/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.repository.nestedrepository;


import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.generator.repository.RepositoryGenContext;
import com.inspur.edp.cef.generator.repository.dac.MainEntityDacGeneratorcs;
import com.inspur.edp.cef.repository.repo.BaseRootRepository;
import com.inspur.edp.cef.repository.repo.RepositoryExtendAttribute;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import lombok.var;
import org.eclipse.jdt.core.dom.TypeLiteral;

import java.util.ArrayList;

public class EntityRepositoryGenerator extends DataTypeRepositoryGenerator {
    public EntityRepositoryGenerator(RepositoryGenContext parentContext) {
        super(parentContext);
    }

    @Override
    protected TypeInfo setParentClass() {
        return new TypeInfo(BaseRootRepository.class.getTypeName());
    }

    @Override
    protected java.util.ArrayList<BaseGenerator> createClassExtendChildGenrators() {
        ArrayList<BaseGenerator> list = super.createClassExtendChildGenrators();
        if (list == null)
            list = new ArrayList<>();
        list.add(getMainDacGenerator());
        return list;
    }

    protected MainEntityDacGeneratorcs getMainDacGenerator() {
        return new MainEntityDacGeneratorcs((IGspCommonObject) parentContext.getCefContext().getCefGenContext().getCommonDataType(), getDataTypeRepoGenContext());
    }

    @Override
    protected ArrayList<AnnotationInfo> getAttributeList() {

        ArrayList<AnnotationInfo> list = null;// super.getAttributeList();
        if (list == null) {
            list = new java.util.ArrayList<AnnotationInfo>();
        }
//        RepositoryExtendAttribute[] extendAttrs =
//                 parentContext.getAssemblerInfo().getBaseAssembler()
//                        .getAssemblerType()
//                        .getAnnotationsByType(RepositoryExtendAttribute.class);


            try {
                if(parentContext.getCefContext().getCefGenContext().getMetadata().getHeader().isExtendable()) {
                    list.add(buildReposExtendInfoAnnotation(Class.forName("com.inspur.edp.customize.repository.CustomizeExtendInfo")));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
//        if (extendAttrs == null || extendAttrs.length < 1)
//            return list;
//        for (var attr : extendAttrs) {
//            list.add(buildReposExtendInfoAnnotation(attr.value()));
//        }

        return list;
    }

    private AnnotationInfo buildReposExtendInfoAnnotation(Class extendClass) {
        AnnotationInfo info = new AnnotationInfo();
        TypeInfo typeInfo = new TypeInfo(RepositoryExtendAttribute.class);
        info.setTypeInfo(typeInfo);
        info.setAnnotationType(AnnotationType.SingleMember);

        TypeLiteral annotationParam = ast.newTypeLiteral();
        annotationParam.setType(ast.newSimpleType(ast.newName(extendClass.getName())));

        info.setSingleParam(annotationParam);
        return info;
    }

}
